package com.mnsq.subject.controller;

import com.mnsq.common.bean.annotation.UserInfoRequired;
import com.mnsq.common.bean.entity.CommonEntity;
import com.mnsq.common.controller.BaseController;
import com.mnsq.common.helper.RedisHelper;
import com.mnsq.common.service.BaseService;
import com.mnsq.subject.bean.condition.SubjectCondition;
import com.mnsq.subject.bean.entity.Subject;
import com.mnsq.subject.service.SubjectService;
import com.mnsq.user.bean.base.ThreadLocalInfo;
import com.mnsq.user.bean.dto.LoginInfo;
import com.vwkit.common.bean.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 周俭
 */
@Api("主题相关接口（讨论话题、议题、面试题）")
@RestController
@RequestMapping("/api/subject/")
public class SubjectController extends BaseController<Subject, BaseService<CommonEntity, SubjectCondition>, SubjectCondition> {

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private RedisHelper redisHelper;

    @PostMapping("/mg/page")
    @UserInfoRequired(required = true)
    public Result page(@RequestBody SubjectCondition subjectCondition) {
        LoginInfo loginInfo = ThreadLocalInfo.LOGIN_INFO.get();
        if (loginInfo.getRoleId() != 1 && loginInfo.getRoleId() != 2) {
            subjectCondition.setCreateUserId(loginInfo.getUserId());
        }
        return super.queryPage(subjectCondition);
    }

    @ApiOperation("详情信息查询")
    @UserInfoRequired
    @GetMapping("/info/detail/{unique}")
    public Result detail(@PathVariable("unique") String unique) {
//        Long userId = ThreadLocalUserId.CURRENT_USER_ID.get();
//        redisHelper.timesRecord(unique, userId, CountRecordType.READ_TIMES.toString());
        return Result.success(subjectService.queryByUnique(unique));
    }

    @ApiOperation("详情点赞、收藏等信息查询")
    @UserInfoRequired(required = true)
    @GetMapping("/info/user/{unique}")
    public Result detailPersonalInfo(@PathVariable("unique") String unique) {
        return Result.success(subjectService.queryUserInfoByUnique(unique));
    }

    @ApiOperation("点赞")
    @UserInfoRequired(required = true)
    @PostMapping("/info/great/{unique}")
    public Result great(@PathVariable("unique") String unique) {
        return subjectService.great(unique);
    }

    @ApiOperation("收藏")
    @UserInfoRequired(required = true)
    @PostMapping("/info/collect/{unique}")
    public Result collect(@PathVariable("unique") String unique) {
        return subjectService.collect(unique);
    }
}
